package com.dropdb.demo.controller.wx;

import com.dropdb.demo.bean.User;
import com.dropdb.demo.bean.bo.*;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.service.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author:tanxin Desc: 购物车service层
 * @date:2020/6/30 10:50
 */
@RestController
@RequestMapping("/wx/cart/")
public class WxCartController {


    @Resource
    WxCartService wxCartService;

    /**
     * 添加至购物车
     * @param addCartBO
     * @return 返回购物车数目
     */
    @RequestMapping("add")
    public BaseRespVO add(@RequestBody AddCartBO addCartBO){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return BaseRespVO.error(501,"请登录");
        }
        //添加
        wxCartService.add2Cart(user.getId(), addCartBO);
        //数量
        int i = wxCartService.queryCartGoodsCount(user.getId());
        return BaseRespVO.ok(i);
    }

    @RequestMapping("fastadd")
    public BaseRespVO fastAdd(@RequestBody AddCartBO addCartBO){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return BaseRespVO.error(501,"请登录");
        }
        BaseRespVO baseRespVO = wxCartService.fastAdd(user.getId(),addCartBO);
        //返回商品在购物车中的cartId
        return baseRespVO;
    }

    /**
     * @return 购物车首页数据
     */
    @RequestMapping("index")
    public BaseRespVO cartIndex(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return BaseRespVO.error(501,"请登录");
        }
        CartDataBO cartDataBO = wxCartService.queryCartIndex(user.getId());
        return  BaseRespVO.ok(cartDataBO);
    }

    /**
     * 购物车商品checked状态更新并返回
     * @param productIds 前端传的待更新的商品ids
     * @return 返回更新后的数据
     */
    @RequestMapping("checked")
    public BaseRespVO cartChecked(@RequestBody ProductIdsBO productIds){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return BaseRespVO.error(501,"请登录");
        }
        CartDataBO cartDataBO = wxCartService.updateCartChecked(user.getId(),productIds);
        return  BaseRespVO.ok(cartDataBO);
    }

    @RequestMapping("update")
    public BaseRespVO updateCart(@RequestBody CartUpdateBO cartUpdateBO){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return BaseRespVO.error(501,"请登录");
        }
        wxCartService.updateCart(user.getId(),cartUpdateBO);
        return BaseRespVO.ok();
    }

    /**
     * @return 购物车商品数量
     */
    @RequestMapping("goodscount")
    public BaseRespVO cartGoodsCount(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return BaseRespVO.error(501,"请登录");
        }
        int goodsCount = wxCartService.queryCartGoodsCount(user.getId());
        return BaseRespVO.ok(goodsCount);
    }

    @RequestMapping("delete")
    public BaseRespVO deleteCart(@RequestBody CartDeleteBO cartDeleteBO){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return BaseRespVO.error(501,"请登录");
        }
        CartDataBO cartDataBO  = wxCartService.deleteCartByProductId(user.getId(),cartDeleteBO);
        return  BaseRespVO.ok(cartDataBO);
    }

    /**
     * @param cartId 购物车id，非空下单购物车所选物品
     * @param addressId 地址id
     * @param couponId 优惠券id
     * @param grouponRulesId 团购id
     * @return 下单页面数据
     */
    @RequestMapping("checkout")
    public BaseRespVO checkout(Integer cartId,Integer addressId,Integer couponId,Integer grouponRulesId){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return BaseRespVO.error(501,"请登录");
        }
        CheckOutDataBO checkOutDataBO  = wxCartService.checkout(user.getId(),cartId, addressId, couponId, grouponRulesId);
        return  BaseRespVO.ok(checkOutDataBO);
    }
}
